#include <sys_config.h>

#ifdef USB_MP_SUPPORT
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <api/libmp/media_player_api.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "images.id"
#include "string.id"
#include "osd_config.h"

#include "control.h"
#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_media.h"
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER    g_win_usb_playlist;

extern TEXT_FIELD usb_playlist_title;

extern OBJLIST	 ol_playlist;
extern SCROLL_BAR	playlist_scb;
extern CONTAINER    playlist_item_con1;
extern CONTAINER    playlist_item_con2;
extern CONTAINER    playlist_item_con3;
extern CONTAINER    playlist_item_con4;
extern CONTAINER    playlist_item_con5;
extern CONTAINER    playlist_item_con6;
extern CONTAINER    playlist_item_con7;
extern CONTAINER    playlist_item_con8;

extern TEXT_FIELD   playlist_fileidx1;
extern TEXT_FIELD   playlist_fileidx2;
extern TEXT_FIELD   playlist_fileidx3;
extern TEXT_FIELD   playlist_fileidx4;
extern TEXT_FIELD   playlist_fileidx5;
extern TEXT_FIELD   playlist_fileidx6;
extern TEXT_FIELD   playlist_fileidx7;
extern TEXT_FIELD   playlist_fileidx8;

extern TEXT_FIELD   playlist_filename1;
extern TEXT_FIELD   playlist_filename2;
extern TEXT_FIELD   playlist_filename3;
extern TEXT_FIELD   playlist_filename4;
extern TEXT_FIELD   playlist_filename5;
extern TEXT_FIELD   playlist_filename6;
extern TEXT_FIELD   playlist_filename7;
extern TEXT_FIELD   playlist_filename8;

extern BITMAP		playlist_fileicon1;
extern BITMAP		playlist_fileicon2;
extern BITMAP		playlist_fileicon3;
extern BITMAP		playlist_fileicon4;
extern BITMAP		playlist_fileicon5;
extern BITMAP		playlist_fileicon6;
extern BITMAP		playlist_fileicon7;
extern BITMAP		playlist_fileicon8;

extern BITMAP		playlist_moveicon1;
extern BITMAP		playlist_moveicon2;
extern BITMAP		playlist_moveicon3;
extern BITMAP		playlist_moveicon4;
extern BITMAP		playlist_moveicon5;
extern BITMAP		playlist_moveicon6;
extern BITMAP		playlist_moveicon7;
extern BITMAP		playlist_moveicon8;

extern TEXT_FIELD   playlist_line1;
extern TEXT_FIELD   playlist_line2;
extern TEXT_FIELD   playlist_line3;
extern TEXT_FIELD   playlist_line4;
extern TEXT_FIELD   playlist_line5;
extern TEXT_FIELD   playlist_line6;
extern TEXT_FIELD   playlist_line7;
extern TEXT_FIELD   playlist_line8;

/*******************************************************************************
*	Function decalare
*******************************************************************************/
#ifndef SD_UI
#define PL_W_L	250
#define PL_W_T	70
#ifdef SUPPORT_CAS_A
#define PL_W_W	516
#else
#define PL_W_W	496
#endif
#define PL_W_H	492
#define PL_GAP_L	18
#define PL_GAP_T    4

#define PL_TITLE_L	(PL_W_L+36)
#define PL_TITLE_T	(PL_W_T + 16)
#define PL_TITLE_W	(PL_W_W-60)
#define PL_TITLE_H	40

#define PL_LST_L	 (PL_W_L + PL_GAP_L)
#define PL_LST_T 	(PL_W_T + 70)
#define PL_LST_W	(PL_W_W - PL_GAP_L * 2)
#define PL_LST_H	 (PL_W_H - 70*2)

#define PL_ITEM_CNT	8
#define PL_ITEM_L	(PL_LST_L + 10)
#define PL_ITEM_T	(PL_LST_T + 0)
#define PL_ITEM_W	(PL_LST_W-30)
#define PL_ITEM_H	40

#define LINE_L_OF	0
#define LINE_T_OF  	PL_ITEM_T
#define LINE_W		PL_ITEM_W
#define LINE_H     	4

#define PL_SCB_L 	(PL_ITEM_L + PL_ITEM_W+4)
#define PL_SCB_T 	PL_ITEM_T
#define PL_SCB_W	 12//18
#define PL_SCB_H	 ((PL_ITEM_H + LINE_H)*PL_ITEM_CNT - LINE_H)

#define PL_ITEM_GAP 4
#define PL_ITEM_IDX_L (PL_ITEM_L + PL_ITEM_GAP)
#define PL_ITEM_IDX_W 80
#define PL_ITEM_ICON_L  (PL_ITEM_IDX_L + PL_ITEM_IDX_W + PL_ITEM_GAP)
#define PL_ITEM_ICON_W 38
#define PL_ITEM_MOVE_W	30
#define PL_ITEM_NAME_L	(PL_ITEM_ICON_L + PL_ITEM_ICON_W + PL_ITEM_GAP)
#define PL_ITEM_NAME_W	(PL_ITEM_W - PL_ITEM_IDX_W - PL_ITEM_ICON_W - PL_ITEM_MOVE_W - PL_ITEM_GAP*5)
#define PL_ITEM_MOVE_L	(PL_ITEM_NAME_L + PL_ITEM_NAME_W + PL_ITEM_GAP)

#define PL_HLP_L (PL_W_L + 30)
#define PL_HLP_T (PL_W_T + PL_W_H - 50)
#define PL_HLP_W  (PL_W_W - 30*2)
#define PL_HLP_H 36
#define PL_HLP_ICON_W 30
#define PL_HLP_TXT_W1 100
#define PL_HLP_TXT_W2 130

#else

#define PL_W_L	100
#define PL_W_T	80
#define PL_W_W	350
#define PL_W_H	350

#define PL_GAP_L	12
#define PL_GAP_T    4

#define PL_TITLE_L	(PL_W_L+36)
#define PL_TITLE_T	(PL_W_T + 8)
#define PL_TITLE_W	(PL_W_W-60)
#define PL_TITLE_H	40

#define PL_LST_L	 (PL_W_L + PL_GAP_L)
#define PL_LST_T 	(PL_W_T + 70)
#define PL_LST_W	(PL_W_W - PL_GAP_L * 2)
#define PL_LST_H	 (PL_W_H - 70*2)

#define PL_ITEM_CNT	6
#define PL_ITEM_L	(PL_LST_L + 8)
#define PL_ITEM_T	(PL_LST_T + 0)
#define PL_ITEM_W	(PL_LST_W-30)
#define PL_ITEM_H	32

#define LINE_L_OF	0
#define LINE_T_OF  	PL_ITEM_T
#define LINE_W		PL_ITEM_W
#define LINE_H     	4

#define PL_SCB_L 	(PL_ITEM_L + PL_ITEM_W+4)
#define PL_SCB_T 	PL_ITEM_T
#define PL_SCB_W	 12//18
#define PL_SCB_H	 ((PL_ITEM_H + LINE_H)*PL_ITEM_CNT - LINE_H)

#define PL_ITEM_GAP 2
#define PL_ITEM_IDX_L (PL_ITEM_L + PL_ITEM_GAP)
#define PL_ITEM_IDX_W 60
#define PL_ITEM_ICON_L  (PL_ITEM_IDX_L + PL_ITEM_IDX_W + PL_ITEM_GAP)
#define PL_ITEM_ICON_W 28
#define PL_ITEM_MOVE_W	30
#define PL_ITEM_NAME_L	(PL_ITEM_ICON_L + PL_ITEM_ICON_W + PL_ITEM_GAP)
#define PL_ITEM_NAME_W	(PL_ITEM_W - PL_ITEM_IDX_W - PL_ITEM_ICON_W - PL_ITEM_MOVE_W - PL_ITEM_GAP*5)
#define PL_ITEM_MOVE_L	(PL_ITEM_NAME_L + PL_ITEM_NAME_W + PL_ITEM_GAP)

#define PL_HLP_L (PL_W_L + 20)
#define PL_HLP_T (PL_W_T + PL_W_H - 48)
#define PL_HLP_W  (PL_W_W - 30*2)
#define PL_HLP_H 36
#define PL_HLP_ICON_W 30
#define PL_HLP_TXT_W1 65
#define PL_HLP_TXT_W2 85

#endif



#define PL_SH_IDX      WSTL_MP_POP_WIN_01_HD
#define PL_TITLE_IDX   WSTL_TEXT_12_HD
#define PL_LST_IDX     WSTL_NOSHOW_IDX
#define PL_CON_SH      WSTL_BUTTON_03_HD//sharon WSTL_MP_TEXT_03_HD
#define PL_CON_HL      WSTL_BUTTON_02_HD//sharon WSTL_BUTTON_03_HD
#define PL_CON_SL      WSTL_BUTTON_08_HD//WSTL_BUTTON_11_HD

#define PL_TXT_SH      WSTL_BUTTON_01_FG_HD//sharon WSTL_MP_TEXT_03_HD
#define PL_TXT_HL	   WSTL_BUTTON_02_FG_HD

#define PL_ICON_SH     WSTL_MIX_BMP_HD

#define PL_SCB_SH_IDX      WSTL_SCROLLBAR_01_8BIT
#define PL_SCB_HL_IDX      WSTL_SCROLLBAR_01_8BIT
#define PL_SCB_MID_RECT_IDX	WSTL_NOSHOW_IDX
#define PL_SCB_MID_THUMB_IDX	WSTL_SCROLLBAR_02_8BIT

#define LIST_STYLE (LIST_VER | LIST_NO_SLECT | LIST_ITEMS_NOCOMPLETE | LIST_SCROLL | LIST_GRID \
                    | LIST_FOCUS_FIRST | LIST_PAGE_KEEP_CURITEM | LIST_FULL_PAGE | LIST_CLIP_ENABLE)

static VACTION playlst_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT playlst_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION playlst_list_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT playlst_list_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
/*******************************************************************************
*	Objects Macro
*******************************************************************************/
#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
	DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, PL_SH_IDX,PL_SH_IDX,PL_SH_IDX,PL_SH_IDX,   \
	playlst_con_keymap,playlst_con_callback,  \
	nxtObj, focusID,0)

#define LDEF_TITLE(root,varTxt,nxtObj,l,t,w,h,resID)		\
	DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, \
	PL_TITLE_IDX,PL_TITLE_IDX,PL_TITLE_IDX,PL_TITLE_IDX,   \
	NULL,NULL,  \
	C_ALIGN_CENTER| C_ALIGN_VCENTER, 4,0,resID,NULL)

#define LDEF_ITEMCON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
	DEF_CONTAINER(varCon,&root,nxtObj,C_ATTR_ACTIVE,0, \
	ID,IDl,IDr,IDu,IDd, l,t,w,h, PL_CON_SH,PL_CON_HL,PL_CON_SL,PL_CON_SH,   \
	NULL,NULL,  \
	conobj, 1,1)

#define LDEF_ITEMTXT(root,varTxt,nxtObj,ID,l,t,w,h,str)		\
	DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
	ID,ID,ID,ID,ID, l,t,w,h, \
	PL_TXT_SH,PL_TXT_HL,PL_TXT_SH,PL_TXT_SH,   \
	NULL,NULL,  \
	C_ALIGN_LEFT | C_ALIGN_VCENTER, 8,0,0,str)
    
#define LDEF_ITEMBMP(root,varBmp,nxtObj,ID,l,t,w,h,bmp_id)		\
	DEF_BITMAP(varBmp,&root, nxtObj, \
	C_ATTR_ACTIVE,C_FONT_DEFAULT, \
	ID, ID, ID, ID, ID, \
	l, t, w, h, \
	PL_ICON_SH, PL_ICON_SH, PL_ICON_SH, PL_ICON_SH, \
	NULL,NULL,  \
	C_ALIGN_CENTER | C_ALIGN_VCENTER, 0, 0, bmp_id)

#define LDEF_POP_LINE(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, WSTL_POP_LINE_HD,WSTL_POP_LINE_HD,WSTL_POP_LINE_HD,WSTL_POP_LINE_HD,   \
    NULL,NULL,  \
    C_ALIGN_RIGHT | C_ALIGN_VCENTER, 10,0,resID,str)


#define LDEF_LIST_ITEM(root,varCon,varIdx,varBmp,varName,varMove,varLine,ID,l,t,w,h,idxstr,bmp_id,namestr,move_id)	\
	LDEF_ITEMCON(root, varCon,NULL,ID,ID,ID,((ID + 1)%PL_ITEM_CNT),((ID - 1)%PL_ITEM_CNT),l,t,w,h,&varIdx,ID)	\
	LDEF_ITEMTXT(varCon,varIdx,&varBmp,ID,PL_ITEM_IDX_L,t,PL_ITEM_IDX_W,h,idxstr)	\
	LDEF_ITEMBMP(varCon,varBmp,&varName,ID,PL_ITEM_ICON_L,t,PL_ITEM_ICON_W,h,bmp_id) \
	LDEF_ITEMTXT(varCon,varName,&varMove,ID,PL_ITEM_NAME_L,t,PL_ITEM_NAME_W,h,namestr)	\
	LDEF_ITEMBMP(varCon,varMove,NULL/*&varLine*/,ID,PL_ITEM_MOVE_L,t,PL_ITEM_MOVE_W,h,move_id) \
	LDEF_POP_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + PL_ITEM_H,LINE_W,LINE_H,0,0)

#define LDEF_LISTBAR(root,varScb,page,l,t,w,h)	\
	DEF_SCROLLBAR(varScb, &root, NULL, C_ATTR_ACTIVE, 0, \
	0, 0, 0, 0, 0, l, t, w, h, \
	PL_SCB_SH_IDX, PL_SCB_SH_IDX, PL_SCB_SH_IDX, PL_SCB_SH_IDX, \
	NULL, NULL, BAR_VERT_AUTO | SBAR_STYLE_RECT_STYLE, \
	page, PL_SCB_MID_THUMB_IDX, PL_SCB_MID_RECT_IDX, \
	0,18, w, h-36, 100, 1)

#define LDEF_OL(root,varOl,nxtObj,l,t,w,h,style,dep,count,flds,sb,mark,selary)	\
	DEF_OBJECTLIST(varOl,&root,nxtObj,C_ATTR_ACTIVE,0, \
	1,1,1,1,1, l,t,w,h,PL_LST_IDX,PL_LST_IDX,PL_LST_IDX,PL_LST_IDX,   \
	playlst_list_keymap,playlst_list_callback,    \
	flds,sb,mark,style,dep,count,selary)
/*******************************************************************************
*	Objects evaluate
*******************************************************************************/
LDEF_WIN(g_win_usb_playlist,&usb_playlist_title,PL_W_L,PL_W_T,PL_W_W,PL_W_H,1)

LDEF_TITLE(g_win_usb_playlist,usb_playlist_title,&ol_playlist,PL_TITLE_L,PL_TITLE_T,PL_TITLE_W,PL_TITLE_H,RS_SHOW_PLAYLIST)

POBJECT_HEAD playlist_items[] =
{
	(POBJECT_HEAD)&playlist_item_con1,
	(POBJECT_HEAD)&playlist_item_con2,
	(POBJECT_HEAD)&playlist_item_con3,
	(POBJECT_HEAD)&playlist_item_con4,
	(POBJECT_HEAD)&playlist_item_con5,
	(POBJECT_HEAD)&playlist_item_con6,
	(POBJECT_HEAD)&playlist_item_con7,
	(POBJECT_HEAD)&playlist_item_con8,
};

LDEF_LISTBAR(ol_playlist,playlist_scb,PL_ITEM_CNT,PL_SCB_L,PL_SCB_T, PL_SCB_W, PL_SCB_H)

LDEF_OL(g_win_usb_playlist,ol_playlist,NULL, PL_LST_L,PL_LST_T,PL_LST_W,PL_LST_H,LIST_STYLE, PL_ITEM_CNT, 0,playlist_items,&playlist_scb,NULL,NULL)

LDEF_LIST_ITEM(ol_playlist,playlist_item_con1,playlist_fileidx1,playlist_fileicon1,playlist_filename1,playlist_moveicon1,playlist_line1,1, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 0),PL_ITEM_W,PL_ITEM_H,display_strs[1],0,display_strs[11],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con2,playlist_fileidx2,playlist_fileicon2,playlist_filename2,playlist_moveicon2,playlist_line2,2, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 1),PL_ITEM_W,PL_ITEM_H,display_strs[2],0,display_strs[12],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con3,playlist_fileidx3,playlist_fileicon3,playlist_filename3,playlist_moveicon3,playlist_line3,3, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 2),PL_ITEM_W,PL_ITEM_H,display_strs[3],0,display_strs[13],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con4,playlist_fileidx4,playlist_fileicon4,playlist_filename4,playlist_moveicon4,playlist_line4,4, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 3),PL_ITEM_W,PL_ITEM_H,display_strs[4],0,display_strs[14],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con5,playlist_fileidx5,playlist_fileicon5,playlist_filename5,playlist_moveicon5,playlist_line5,5, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 4),PL_ITEM_W,PL_ITEM_H,display_strs[5],0,display_strs[15],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con6,playlist_fileidx6,playlist_fileicon6,playlist_filename6,playlist_moveicon6,playlist_line6,6, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 5),PL_ITEM_W,PL_ITEM_H,display_strs[6],0,display_strs[16],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con7,playlist_fileidx7,playlist_fileicon7,playlist_filename7,playlist_moveicon7,playlist_line7,7, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 6),PL_ITEM_W,PL_ITEM_H,display_strs[7],0,display_strs[17],IM_TV_MOVE)
LDEF_LIST_ITEM(ol_playlist,playlist_item_con8,playlist_fileidx8,playlist_fileicon8,playlist_filename8,playlist_moveicon8,playlist_line8,8, \
	PL_ITEM_L,(PL_ITEM_T + (PL_ITEM_H + PL_GAP_T) * 7),PL_ITEM_W,PL_ITEM_H,display_strs[8],0,display_strs[18],IM_TV_MOVE)

TEXT_FIELD *playlist_idxs[] = 
{
	&playlist_fileidx1,
	&playlist_fileidx2,
	&playlist_fileidx3,
	&playlist_fileidx4,
	&playlist_fileidx5,
	&playlist_fileidx6,
	&playlist_fileidx7,
	&playlist_fileidx8,
};
TEXT_FIELD *playlist_files[] = 
{
	&playlist_filename1,
	&playlist_filename2,
	&playlist_filename3,
	&playlist_filename4,
	&playlist_filename5,
	&playlist_filename6,
	&playlist_filename7,
	&playlist_filename8,
};
BITMAP *playlist_icons[] = 
{
	&playlist_fileicon1,
	&playlist_fileicon2,
	&playlist_fileicon3,
	&playlist_fileicon4,
	&playlist_fileicon5,
	&playlist_fileicon6,
	&playlist_fileicon7,
	&playlist_fileicon8,
};
BITMAP *playlist_moveicons[] = 
{
	&playlist_moveicon1,
	&playlist_moveicon2,
	&playlist_moveicon3,
	&playlist_moveicon4,
	&playlist_moveicon5,
	&playlist_moveicon6,
	&playlist_moveicon7,
	&playlist_moveicon8,
};

extern UINT16 file_icon_ids[];

#define VACT_PLAY (VACT_PASS + 1)
#define VACT_DELETE  (VACT_PASS + 2)
#define VACT_MOVE (VACT_PASS + 3)
#define VACT_DELETE_ALL (VACT_PASS + 4)

#define INVALID_IDX  0xFFFF

static PlayListHandle cur_display_playlist;
static WIN_PLAYLIST_TYPE cur_playlist_type;
UINT8  moving_flag;
UINT16 move_file_idx;
extern BOOL	musiclist_changed, imagelist_changed;
extern PlayListHandle win_play_list[WIN_PLAYLIST_NUMBER];

static void win_open_playlist_help(void);
static PRESULT playlist_colorkey_proc(VACTION unact);
static void playlist_display_items(UINT16 uTop);
/*******************************************************************************
*	Function
*******************************************************************************/
static VACTION playlst_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
	
	}

	return act;
}
static PRESULT playlst_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	UINT16 title_strid;
	PRESULT ret = PROC_PASS;
	POBJLIST pOl;
	TEXT_FIELD *pTxt;
	UINT32 file_num;
	win_popup_choice_t choice;
	UINT8 back_saved;
	UINT16 i;
	WIN_PLAYLIST_TYPE play_list;

	pOl = &ol_playlist;
	switch(event)
	{
   	case EVN_PRE_OPEN:
		get_play_list_info(cur_display_playlist,&file_num, NULL);
		OSD_SetObjListCount(pOl, file_num);
		OSD_SetObjListCurPoint(pOl, 0);
		OSD_SetObjListNewPoint(pOl, 0);
		OSD_SetObjListTop(pOl, 0);
		moving_flag = 0;
		move_file_idx = INVALID_IDX;
		break;
	case EVN_POST_OPEN:
		win_open_playlist_help();
		break;
   	case EVN_PRE_CLOSE:
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;
	case EVN_POST_CLOSE:
		break;
	default:
		break;
	}

	return ret;
}

static VACTION playlst_list_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	switch(key)
	{
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;			
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;			
	case V_KEY_P_UP:
		act = VACT_CURSOR_PGUP;
		break;			
	case V_KEY_P_DOWN:
		act = VACT_CURSOR_PGDN;
		break;
	case V_KEY_ENTER:
		act = VACT_PLAY;
		break;
	case V_KEY_RED:
		act = VACT_MOVE;
		break;
	case V_KEY_GREEN:
		act = VACT_DELETE;
		break;
	case V_KEY_YELLOW:
		act = VACT_DELETE_ALL;
		break;
	default:
		act = VACT_PASS;
		break;
	}

	return act;		
}
static PRESULT playlst_list_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	UINT8 bID;
	UINT16 uTop,uCnt,uCurpos;
	PRESULT ret = PROC_PASS;
	VACTION unact;
    POBJLIST ol = &ol_playlist;
	POBJECT_HEAD pSelItem = NULL;    

	uCnt = OSD_GetObjListCount((POBJLIST)pObj);
	uTop = OSD_GetObjListTop((POBJLIST)pObj);
	uCurpos = OSD_GetObjListCurPoint((POBJLIST)pObj);
	bID = uCurpos - uTop;
	switch(event)
	{
		case EVN_PRE_DRAW:
			ret = PROC_LOOP;
			if(param1 == C_DRAW_TYPE_HIGHLIGHT)
			{
				playlist_display_items(uTop);
				OSD_TrackObject(pObj, C_UPDATE_ALL);
                 if((ol->wSelect >= ol->wTop)
                	&&(ol->wSelect < (ol->wTop + ol->wDep))
                	&&(ol->wSelect != ol->wCurPoint))
                {
                	if(ol->bListStyle & LIST_ITEMS_COMPLETE)
                	{
                		pSelItem = ol->pListField[ol->wSelect];
                	}
                	else
                	{
                		pSelItem = ol->pListField[ol->wSelect - ol->wTop];
                	}
                	OSD_SelObject(pSelItem, C_UPDATE_ALL);
                }  
			}
			break;
		case EVN_POST_DRAW:
			break;
		case EVN_ITEM_PRE_CHANGE:
			break;
		case EVN_ITEM_POST_CHANGE:
			break;
		case EVN_UNKNOWN_ACTION:
			unact = (VACTION)(param1>>16);
			ret = playlist_colorkey_proc(unact);
			break;
		default:
			break;
	}
	
	return ret;
}

void win_playlist_open(WIN_PLAYLIST_TYPE playlist_type)
{
	UINT32 hkey;
	VSCR vscr;
	CONTAINER	*pop_win;
	TEXT_FIELD *pTxt;
	PRESULT ret = PROC_LOOP;

    BOOL old_value = ap_enable_key_task_get_key(TRUE);

	cur_playlist_type = playlist_type;
	cur_display_playlist = win_play_list[playlist_type];
		
	pop_win = &g_win_usb_playlist;
	OSD_SetContainerFocus(pop_win, 1);
	wincom_backup_region(&pop_win->head.frame);
	OSD_ObjOpen((POBJECT_HEAD)pop_win, 0);
	while(ret != PROC_LEAVE)
	{
		hkey = ap_get_key_msg();
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
			continue;

		ret = OSD_ObjProc((POBJECT_HEAD)pop_win, (MSG_TYPE_KEY<<16),hkey, 0);
	}
	mp_set_fav_list(0);
	wincom_restore_region();
    ap_enable_key_task_get_key(old_value);
}

static void win_open_playlist_help(void)
{
	UINT8 i;
	UINT16 hlp_imgs[] =
	{
		IM_EPG_COLORBUTTON_RED,
		IM_EPG_COLORBUTTON_GREEN,
		IM_EPG_COLORBUTTON_YELLOW,
	};

	UINT16 hlp_strs[] = {
		RS_HELP_MOVE,
		RS_HELP_DELETE,
		RS_DELETE_ALL};


	DEF_BITMAP(hlp_bmp,NULL, NULL, \
	C_ATTR_ACTIVE,C_FONT_DEFAULT, \
	0, 0, 0, 0, 0, \
	0,0,0,0, \
	WSTL_MIX_BMP_HD, WSTL_MIX_BMP_HD, WSTL_MIX_BMP_HD, WSTL_MIX_BMP_HD, \
	NULL,NULL,  \
	C_ALIGN_LEFT| C_ALIGN_VCENTER, 0, 0,0)
	
	DEF_TEXTFIELD(hlp_txt,NULL,NULL,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, \
	0,0,0,0, \
	WSTL_TEXT_12_HD,WSTL_TEXT_12_HD,WSTL_TEXT_12_HD,WSTL_TEXT_12_HD,    \
	NULL,NULL,  \
	C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,RS_COMMON_OK,NULL)
	
	for(i = 0; i < 2;i++)
	{
		OSD_SetObjRect((POBJECT_HEAD)&hlp_bmp, \
			PL_HLP_L+i*(PL_HLP_ICON_W+PL_HLP_TXT_W1), PL_HLP_T, PL_HLP_ICON_W, PL_HLP_H);
		OSD_SetBitmapContent(&hlp_bmp, hlp_imgs[i]);
		OSD_DrawObject((POBJECT_HEAD)&hlp_bmp,C_UPDATE_ALL);

		OSD_SetObjRect((POBJECT_HEAD)&hlp_txt, \
			PL_HLP_L+PL_HLP_ICON_W+i*(PL_HLP_ICON_W+PL_HLP_TXT_W1), PL_HLP_T, PL_HLP_TXT_W1, PL_HLP_H);
		OSD_SetTextFieldContent(&hlp_txt,STRING_ID,hlp_strs[i]);
		OSD_DrawObject((POBJECT_HEAD)&hlp_txt,C_UPDATE_ALL);
	}
	//show  delete all
	OSD_SetObjRect((POBJECT_HEAD)&hlp_bmp, \
		PL_HLP_L+2*(PL_HLP_ICON_W+PL_HLP_TXT_W1), PL_HLP_T, PL_HLP_ICON_W, PL_HLP_H);
	OSD_SetBitmapContent(&hlp_bmp, hlp_imgs[2]);
	OSD_DrawObject((POBJECT_HEAD)&hlp_bmp,C_UPDATE_ALL);

	OSD_SetObjRect((POBJECT_HEAD)&hlp_txt, \
		PL_HLP_L+PL_HLP_ICON_W+2*(PL_HLP_ICON_W+PL_HLP_TXT_W1), PL_HLP_T, PL_HLP_TXT_W2, PL_HLP_H);
	OSD_SetTextFieldContent(&hlp_txt,STRING_ID,hlp_strs[2]);
	OSD_DrawObject((POBJECT_HEAD)&hlp_txt,C_UPDATE_ALL);
	
}

static PRESULT playlist_colorkey_proc(VACTION unact)
{
	UINT16 curPos;
	PRESULT ret_proc = PROC_PASS;
	POBJLIST pOl;
	POBJECT_HEAD top_win;
	UINT8 back_saved;
	win_popup_choice_t choice;
	UINT32 file_num;
	
	pOl = &ol_playlist;
	curPos = OSD_GetObjListCurPoint(pOl);
	switch(unact)
	{
		case VACT_PLAY:
			top_win = (POBJECT_HEAD)(&g_win_usb_filelist);
			if(cur_playlist_type == PL_MUSIC)
			{
				win_set_musicplay_param(&cur_display_playlist,curPos+1,0);
				ap_send_msg(CTRL_MSG_SUBTYPE_CMD_PLAYMUSIC,(UINT32)top_win,TRUE);
			}
			else
			{
				win_set_imageplay_param(&cur_display_playlist, curPos+1,0);
				ap_send_msg(CTRL_MSG_SUBTYPE_CMD_PLAYIMAGE,(UINT32)top_win,TRUE);
			}
			ret_proc = PROC_LEAVE;
			break;
		case VACT_MOVE:
			if(moving_flag == 0)
			{
				moving_flag = 1;
				move_file_idx = curPos;
			}
			else
			{
				moving_flag = 0;
				//move file
				move_file_in_play_list(cur_display_playlist, move_file_idx+1, curPos+1);
				move_file_idx = INVALID_IDX;
			}
   			OSD_TrackObject((POBJECT_HEAD)pOl, C_UPDATE_ALL | C_DRAW_SIGN_EVN_FLG);
			break;
		case VACT_DELETE:
			if(moving_flag == 1)
			{
				moving_flag = 0;
				//move file
				move_file_in_play_list(cur_display_playlist, move_file_idx+1, curPos+1);
				move_file_idx = INVALID_IDX;
			}
			delete_play_list_item(cur_display_playlist, curPos+1);

			if(cur_playlist_type == PL_MUSIC)
			{
				musiclist_changed = TRUE;
			}
			else
			{
				imagelist_changed = TRUE;
			}
			get_play_list_info(cur_display_playlist,&file_num, NULL);
			OSD_SetObjListCount(pOl, file_num);
   			if(file_num == 0)//all items have been deleted
   			{
 				delete_play_list(win_play_list[cur_playlist_type]);
				win_play_list[cur_playlist_type] = NULL;
  				ret_proc = PROC_LEAVE;
   				break;
   			}
   			if(curPos >= file_num)
   			{
   				OSD_SetObjListCurPoint(pOl, 0);
   				OSD_SetObjListNewPoint(pOl, 0);
   				OSD_SetObjListTop(pOl, 0);
   			}
   			OSD_TrackObject((POBJECT_HEAD)pOl, C_UPDATE_ALL | C_DRAW_SIGN_EVN_FLG);
			break;
		case VACT_DELETE_ALL:
 			win_compopup_init(WIN_POPUP_TYPE_OKNO);
 			win_compopup_set_msg(NULL,NULL,RS_MP_DELETE_PLAYLIST);
 			choice = win_compopup_open_ext(&back_saved);		
 			if(choice == WIN_POP_CHOICE_YES)
 			{
				delete_play_list(win_play_list[cur_playlist_type]);
				win_play_list[cur_playlist_type] = NULL;
				if(cur_playlist_type == PL_MUSIC)
				{
					musiclist_changed = TRUE;
				}
				else
				{
					imagelist_changed = TRUE;
				}
	   			ret_proc = PROC_LEAVE;
 			}
			break;
		default:
			break;
	}

	return ret_proc;
}


static UINT16 list_index_2_playlist_index(OBJLIST* ol,UINT16 list_idx)
{
    char flag = 0;
    UINT16 start_idx,end_idx,cur_idx,plist_idx,new_point;

    /* When moving, the file displayer order is not same as normal */
    if(moving_flag == 1 && move_file_idx!=INVALID_IDX)
    {
        new_point = OSD_GetObjListNewPoint(ol);//ol->wNewPoint;      
        start_idx = (new_point>move_file_idx) ? move_file_idx : new_point;
        end_idx   = (new_point>move_file_idx) ? new_point : move_file_idx;
        
        if( list_idx<start_idx ||  list_idx> end_idx)
            flag = 0;
        else
        {
            if(new_point==move_file_idx)
                flag = 0;
            else if(new_point<move_file_idx)
            {
                if( list_idx>new_point && list_idx<=move_file_idx)
                    flag = -1;
                //else    ;//list_idx==new_point)
            }
            else
            {
                if( list_idx>=move_file_idx && list_idx<new_point)
                    flag = 1;
                //else  ;  //list_idx==new_point)                    
            }
        }
            
        if(list_idx==new_point)  /* foucs item show the channel in moving */
            plist_idx = move_file_idx;
        else
            plist_idx = list_idx + flag;
    }
    else    /* not in moving state*/
        plist_idx = list_idx;

   return plist_idx;

}

static void playlist_display_items(UINT16 uTop)
{
	UINT8 i;
	char str_buff[MAX_FILE_NAME_SIZE + 1];
    char filename[MAX_FILE_NAME_SIZE] = {0};
    char *current_music_name = NULL;
	RET_CODE ret;
	TEXT_FIELD *pTxt;
	BITMAP *pBmp;
	FileInfo playlist_node;
	BOOL move_flag;
	UINT16 file_idx, curitem, index;
	POBJLIST pOl;
	#ifdef PERSIAN_SUPPORT
	UINT16 str[64];		
	char str_uni[128];
	#endif

	pOl = &ol_playlist;
	curitem = OSD_GetObjListNewPoint(pOl);
    current_music_name = get_current_music_name();
    OSD_SetObjListSingleSelect(&ol_playlist, 0xFFFF);//reset object list select flag
	for(i = 0; i < PL_ITEM_CNT;i++)
	{
		index = uTop + i;
		move_flag = FALSE;
		if((moving_flag==1) && (move_file_idx != INVALID_IDX) && (index == curitem))
		{
			move_flag = TRUE;
		}

		file_idx = list_index_2_playlist_index(pOl, index);
		ret = get_file_from_play_list(cur_display_playlist,(file_idx +1),&playlist_node);

		if(ret == RET_SUCCESS)
		{
			pTxt = playlist_idxs[i];
			sprintf(str_buff,"%d",(uTop + i + 1));
			OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)str_buff);
			pBmp = playlist_icons[i];
			OSD_SetBitmapContent(pBmp, file_icon_ids[playlist_node.filetype]);
			pTxt = playlist_files[i];
			#ifdef PERSIAN_SUPPORT			
			filelist_utf8_to_unicode(playlist_node.name, str_uni);
  			ComUniStrCopyChar( (UINT8 *)str, str_uni);
			OSD_SetTextFieldContent(pTxt, STRING_UNICODE,(UINT32)str);
			#else
			//filter_unkownfont_name(playlist_node.name,str_buff);
			win_shorten_filename(playlist_node.name,str_buff);
			OSD_SetTextFieldContent(pTxt,STRING_UTF8,(UINT32)str_buff);
			#endif
			pBmp = playlist_moveicons[i];
			if(move_flag == TRUE)
				//OSD_SetBitmapContent(pBmp, IM_TV_DEL);
				OSD_SetAttr(pBmp, C_ATTR_ACTIVE);				
			else
				OSD_SetAttr(pBmp, C_ATTR_HIDDEN);	

			win_get_fullname(filename, playlist_node.path, playlist_node.name);
			if(!STRCMP(current_music_name, filename))
		    {
    			OSD_SetObjListSingleSelect(&ol_playlist, uTop + i);
		    }
            
		}
		else
		{
			pTxt = playlist_idxs[i];
			OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"");
			pBmp = playlist_icons[i];
			OSD_SetBitmapContent(pBmp, 0);
			pTxt = playlist_files[i];
			OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"");
			pBmp = playlist_moveicons[i];
			OSD_SetAttr(pBmp, C_ATTR_HIDDEN);	
		}
	}
}

#endif

